package com.gxa.jjty.admin.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.service.*;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.pojo.*;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.sql.Timestamp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j2
@Api(tags = "残疾人管理接口")
@RestController
@RequestMapping("/handicapped")
@Transactional(rollbackFor = Throwable.class)
@Validated
public class HandicappedController {

    @Autowired
    HandicappedService handicappedService;

   @Autowired
    KinshipService kinshipService;

   @Autowired
    CodeService codeService;
   @Autowired
    TorderService torderService;
   @Autowired
    HealthyService healthyService;
   @Autowired
    RedisTemplate redisTemplate;

   @PostMapping("/getcode")
   @ApiOperation("自动生成code")
   public Map<String,Object> getCode() throws IllegalAccessException {
       Code code = codeService.selectById(1);
       String prefix = code.getPrefix();
       System.out.println("prefix:"+prefix);
       Long length = code.getLength();
       Long num = code.getNum();
       String s = prefix;
       for(int i=0;i<(length-num.toString().length());i++){
           s+="0";
       }
       s = s+num.toString() ;
       code.setNum(num++);
       System.out.println(code);
       codeService.updateById(code);
       JsonResult jsonResult = new JsonResult();
       jsonResult.setCode("1");
       jsonResult.setData(s);
       return jsonResult.getDataMap();
   }



    @PostMapping("/add")
    @ApiOperation("添加用户")
    public Map<String,Object> addHandicapped(@RequestBody Handicapped handicapped) {
       handicapped.setRegTime(new Timestamp(System.currentTimeMillis()));
        Handicapped insert = handicappedService.insert(handicapped);
        List<Kinship> list = handicapped.getKinship();
        for (Kinship kinship : list) {
            kinshipService.insert(kinship);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();

    }
  @PostMapping("/selectById")
  @ApiOperation("单个用户查询")
    public Map<String,Object> selectById(@RequestParam(name = "id") Long id){
       //残疾人基本信息
        Handicapped handicapped = handicappedService.selectById(id);
      Kinship kinship = new Kinship();
      kinship.setHandicappedId(id);
      //残疾人亲属关系
      List<Kinship> kinshipList = kinshipService.select(kinship);
      //残疾人健康状态
      Healthy healthy = new Healthy();
      healthy.setId(id);
      List<Healthy> healthies = healthyService.select(healthy);
      //残疾人所有订单
      Torder torder = new Torder();
      torder.setCjrId(id);
      List<Torder> torders = torderService.select(torder);
      Map map = new HashMap();
      map.put("handicapped",handicapped);
      map.put("kinshipList",kinshipList);
      map.put("healthies",healthies);
      map.put("torders",torders);
      JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(map);
        return jsonResult.getDataMap();
    }
    @PostMapping("/delete")
    @ApiOperation("删除")
    public Map<String,Object> deleteById(@RequestParam(name = "id") Long id){
        Integer integer = handicappedService.deleteById(id);
        redisTemplate.delete("cjrUser:"+id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }
    @PostMapping("/update")
    @ApiOperation("修改")
    public Map<String,Object> update(@RequestBody Handicapped handicapped) throws IllegalAccessException {
        Long id = handicapped.getId();
        Integer integer = handicappedService.updateById(handicapped);
        redisTemplate.delete("cjrUser:"+id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }
    @PostMapping("/selectAll")
    @ApiOperation("全查询")
    public Map<String,Object> selectAll(){
        List<Handicapped> list = handicappedService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(list);
        return jsonResult.getDataMap();
    }
    @PostMapping("/selectByPage")
    @ApiOperation("分页查询")
    public Map<String,Object> selectByPage(@RequestParam(name = "page") Integer page,@RequestParam(name = "size") Integer size) throws IllegalAccessException {
        PageInfo<Handicapped> handicappedPageInfo = handicappedService.selectByPage(page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(handicappedPageInfo);
        return jsonResult.getDataMap();
    }

    @PostMapping("/selectByField")
    @ApiOperation("条件查询")
    public Map selectByField(String from, String to, String code, String name, String phone, String state){
        List<Handicapped> handicappeds = handicappedService.selectByFiedl(from, to, code, name, phone, state);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(handicappeds);
        return jsonResult.getDataMap();
    }


}
